﻿using FangChan.Common;
using McsWeb.Models;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace McsWeb.Controllers
{
    public class BackStaffController : Controller
    {
        // GET: BackStaff
        public ActionResult Index()
        {
            return View();
        }

        public string IndexAsync(string staffID, string currencys, string beginTime, string endTime)
        {
            if (staffID == null || staffID == "")
            {
                staffID = "0";
            }

            if (currencys == null)
            {
                currencys = "";
            }

            List<BackStaffCalculatingModel> bscm = new List<BackStaffCalculatingModel>();


            DataTable dt = FangChan.DBUtility.DbHelperSQL.Query("select * from Fn_Table_BackStaffCalculating(" + Convert.ToInt32(staffID) + ",'" + currencys + "','" + beginTime + "','" + endTime + "')").Tables[0];

            int total = Convert.ToInt32(FangChan.DBUtility.DbHelperSQL.GetSingle("select count(*) from Fn_Table_BackStaffCalculating(" + Convert.ToInt32(staffID) + ",'" + currencys + "','" + beginTime + "','" + endTime + "')"));

            foreach (DataRow item in dt.Rows)
            {
                BackStaffCalculatingModel b = new BackStaffCalculatingModel();

                b.CalculatingDay = Convert.ToString(item["CalculatingDay"]);
                b.Currencys = Convert.ToString(item["Currencys"]);
                b.Xiaozhangyu = Convert.ToString(item["小章鱼"]);
                b.Xiaoqiao = Convert.ToString(item["小乔"]);
                b.Dawei = Convert.ToString(item["大伟"]);
                b.Xiaowu = Convert.ToString(item["小五"]);
                b.Xiaohui = Convert.ToString(item["小辉"]);
                b.Xiaoliang = Convert.ToString(item["小亮"]);
                b.Xiaoran = Convert.ToString(item["萧然"]);

                bscm.Add(b);
            }

            GridData<BackStaffCalculatingModel> gridData = new GridData<BackStaffCalculatingModel>() { total = total, rows = bscm };

            return gridData.ToJson();

        }

        public ActionResult DayIndex()
        {
            return View();
        }

        public string DayIndexAsync(string staffID, string currencys, string beginTime, string endTime)
        {
            if (staffID == null || staffID == "")
            {
                staffID = "0";
            }

            if (currencys == null)
            {
                currencys = "";
            }

            List<BackStaffCalculating4DayModel> bscdm = new List<BackStaffCalculating4DayModel>();

            DataTable dt = FangChan.DBUtility.DbHelperSQL.Query("select * from Fn_Table_BackStaffCalculating4Day(" + Convert.ToInt32(staffID) + ",'" + currencys + "','" + beginTime + "','" + endTime + "')").Tables[0];

            int total = Convert.ToInt32(FangChan.DBUtility.DbHelperSQL.GetSingle("select count(*) from Fn_Table_BackStaffCalculating4Day(" + Convert.ToInt32(staffID) + ",'" + currencys + "','" + beginTime + "','" + endTime + "')"));

            foreach (DataRow item in dt.Rows)
            {
                BackStaffCalculating4DayModel b = new BackStaffCalculating4DayModel();

                b.CalculatingDay = Convert.ToString(item["CalculatingDay"]);
                b.Currencys = Convert.ToString(item["Currencys"]);
                b.StaffID = Convert.ToInt32(item["StaffID"]);
                b.StaffName = Convert.ToString(item["StaffName"]);
                b.Calculating = Convert.ToString(item["Calculating"]);
                b.Number = Convert.ToInt32(item["Number"]);

                bscdm.Add(b);
            }

            GridData<BackStaffCalculating4DayModel> gridData = new GridData<BackStaffCalculating4DayModel>() { total = total, rows = bscdm };

            return gridData.ToJson();

        }

        public ActionResult HourIndex(string StaffID, string Currencys, string BeginTime, string EndTime)
        {
            ViewBag.StaffID = StaffID;
            ViewBag.Currencys = Currencys;
            ViewBag.BeginTime = BeginTime;
            ViewBag.EndTime = EndTime;
            return View();
        }

        public string HourIndexAsync(string staffID, string currencys, string beginTime, string endTime)
        {
            if (staffID == null || staffID == "")
            {
                staffID = "0";
            }

            if (currencys == null)
            {
                currencys = "";
            }

            List<BackStaffCalculating4DayModel> bscdm = new List<BackStaffCalculating4DayModel>();
            string sql = "select * from Fn_Table_BackStaffCalculating4Hour(" + Convert.ToInt32(staffID) + ",'" + currencys + "','" + beginTime + "','" + endTime + "')";

            DataTable dt = FangChan.DBUtility.DbHelperSQL.Query(sql).Tables[0];

            int total = Convert.ToInt32(FangChan.DBUtility.DbHelperSQL.GetSingle("select count(*) from Fn_Table_BackStaffCalculating4Hour(" + Convert.ToInt32(staffID) + ",'" + currencys + "','" + beginTime + "','" + endTime + "')"));

            foreach (DataRow item in dt.Rows)
            {
                BackStaffCalculating4DayModel b = new BackStaffCalculating4DayModel();

                b.CalculatingDay = Convert.ToString(item["CalculatingDay"]);
                b.Currencys = Convert.ToString(item["Currencys"]); 
                b.Calculating = Convert.ToString(item["Calculating"]);
                b.Number = Convert.ToInt32(item["Number"]);

                bscdm.Add(b);
            }

            GridData<BackStaffCalculating4DayModel> gridData = new GridData<BackStaffCalculating4DayModel>() { total = total, rows = bscdm };

            return gridData.ToJson();

        }

        //角色
        public string GetStaff()
        {
            DataTable dt = FangChan.DBUtility.DbHelperSQL.Query("select ID,Name from Staffs where ID !=1").Tables[0];

            return dt.DataTable2Json();
        }

        //币种
        public string GetCurrencys()
        {
            DataTable dt = FangChan.DBUtility.DbHelperSQL.Query("select ID,Name from Currencys where Type=1").Tables[0];

            return dt.DataTable2Json();
        }
    }
}